package com.example.bingobox.ui.todobox;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SceneSelectionAdapter extends RecyclerView.Adapter<SceneSelectionAdapter.ViewHolder> {
    private final Map<String, List<String>> sceneGroups = new LinkedHashMap<>();
    private final Map<String, Boolean> expandedStates = new LinkedHashMap<>();
    private final OnSceneSelectedListener listener;
    private final Set<String> selectedScenes;

    public interface OnSceneSelectedListener {
        void onSceneSelected(String scene, boolean selected);
    }

    public SceneSelectionAdapter(Set<String> selectedScenes, OnSceneSelectedListener listener) {
        this.selectedScenes = selectedScenes;
        this.listener = listener;
    }

    public void setScenes(List<String> scenes) {
        sceneGroups.clear();
        expandedStates.clear();
        
        // 按大场景分组
        for (String scene : scenes) {
            String[] parts = scene.split("-");
            if (parts.length == 2) {
                String majorScene = parts[0];
                String minorScene = parts[1];
                sceneGroups.computeIfAbsent(majorScene, k -> new ArrayList<>()).add(scene);
                expandedStates.put(majorScene, false);
            }
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_scene_selection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String majorScene = new ArrayList<>(sceneGroups.keySet()).get(position);
        List<String> minorScenes = sceneGroups.get(majorScene);
        boolean isExpanded = expandedStates.get(majorScene);

        holder.majorSceneText.setText(majorScene);
        holder.majorSceneText.setOnClickListener(v -> {
            expandedStates.put(majorScene, !isExpanded);
            notifyItemChanged(position);
        });

        holder.minorScenesGroup.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        if (isExpanded) {
            holder.minorScenesGroup.removeAllViews();
            for (String scene : minorScenes) {
                Chip chip = new Chip(holder.minorScenesGroup.getContext());
                chip.setText(scene.split("-")[1]);
                chip.setCheckable(true);
                chip.setChecked(selectedScenes.contains(scene));
                chip.setOnCheckedChangeListener((buttonView, isChecked) -> {
                    if (listener != null) {
                        listener.onSceneSelected(scene, isChecked);
                    }
                });
                holder.minorScenesGroup.addView(chip);
            }
        }
    }

    @Override
    public int getItemCount() {
        return sceneGroups.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView majorSceneText;
        ChipGroup minorScenesGroup;

        ViewHolder(View itemView) {
            super(itemView);
            majorSceneText = itemView.findViewById(R.id.text_major_scene);
            minorScenesGroup = itemView.findViewById(R.id.chip_group_minor_scenes);
        }
    }
} 